package com.mywork.mvpkodeinxml.utils

import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.mywork.mvpkodeinxml.app.App
import com.mywork.mvpkodeinxml.common.PREFS


/*
    SP工具类
 */
object AppPrefsUtils {
    private var sp: SharedPreferences = App.instance.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
    private var ed: Editor

    init {
        ed = sp.edit()
    }

    /*
        Boolean数据
     */
    fun putBoolean(key: String, value: Boolean) {
        ed.putBoolean(key, value)
        ed.commit()
    }

    /*
        默认 false
     */
    fun getBoolean(key: String): Boolean {
        return sp.getBoolean(key, false)
    }

    /*
        String数据
     */
    fun putString(key: String, value: String) {
        ed.putString(key, value)
        ed.commit()
    }

    /*
        默认 ""
     */
    fun getString(key: String): String {
        return sp.getString(key, "") ?: ""
    }

    /*
        Int数据
     */
    fun putInt(key: String, value: Int) {
        ed.putInt(key, value)
        ed.commit()
    }

    /*
        默认 0
     */
    fun getInt(key: String): Int {
        return sp.getInt(key, 0)
    }

    /*
        Long数据
     */
    fun putLong(key: String, value: Long) {
        ed.putLong(key, value)
        ed.commit()
    }

    /*
        默认 0
     */
    fun getLong(key: String): Long {
        return sp.getLong(key, 0)
    }

    /*
        Set数据
     */
    fun putStringSet(key: String, set: Set<String>) {
        val localSet = getStringSet(key).toMutableSet()
        localSet.addAll(set)
        ed.putStringSet(key, localSet)
        ed.commit()
    }

    /*
        默认空set
     */
    fun getStringSet(key: String): Set<String> {
        val set = setOf<String>()
        return sp.getStringSet(key, set) ?: set
    }

    /*
        删除key数据
     */
    fun remove(key: String) {
        ed.remove(key)
        ed.commit()
    }

    /**
     * 用于保存集合
     *
     * @param key key
     * @param map map数据
     */
    fun <K, V> putHashMapData(key: String, map: HashMap<K, V>): Boolean {
        var result: Boolean

        val editor = sp.edit()
        try {
            val gson = Gson()
            val json = gson.toJson(map)
            editor.putString(key, json)
            result = true
        } catch (e: Exception) {
            result = false
            e.printStackTrace()
        }

        editor.apply()
        return result
    }

    /**
     * 用于取出集合
     *
     * @param key key
     * @param clsV object class
     * @return HashMap
     */
    fun <V> getHashMapData(key: String,clsV: Class<V>): HashMap<String, V> {
        val json = sp.getString(key, "")
        val map = HashMap<String, V>()
        val gson = Gson()
        if (json.isNullOrEmpty()){
            return map
        }
        try {
            val obj = JsonParser().parse(json).asJsonObject
            val entrySet = obj.entrySet()
            for (entry in entrySet) {
                val entryKey = entry.key
                val value = entry.value as JsonObject
                map[entryKey] = gson.fromJson(value, clsV)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return map
    }

}
